package com.antt.cn.test2.uis.activities;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.antt.cn.test2.R;
import com.antt.cn.test2.uis.base.BaseActivity;
import com.antt.cn.test2.utils.AntLog;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by baichuan on 2017/4/24.
 */

public class Test02 extends BaseActivity {

    @BindView(R.id.ac_test02_obt01)
    Button obt;
    @BindView(R.id.ac_test02_text)
    TextView text;

    @Override
    public int getLayoutId() {
        return R.layout.ac_test02_layout;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    @OnClick(R.id.ac_test02_obt01)
    public void obtClick(View v) {
        test6();
    }


    public void test1() {
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                AntLog.i("onSubscribe");
            }

            @Override
            public void onNext(@NonNull String s) {
                AntLog.i("onNext");
            }

            @Override
            public void onError(@NonNull Throwable e) {
                AntLog.i("OnError");
            }

            @Override
            public void onComplete() {
                AntLog.i("onComplete");
            }
        };
    }

    public void test2() {
        Subscriber<String> subscriber = new Subscriber<String>() {


            @Override
            public void onSubscribe(Subscription s) {

            }

            @Override
            public void onNext(String s) {

            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        };

    }

    public void test3() {

        // create productor
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                e.onNext("hello");
                e.onNext("word");
                e.onNext("ant");
                e.onNext("antsdfasdkfalsjdfkas");
                e.onComplete();
            }
        });

        // create consumer
        Observer<String> observer = new Observer<String>() {

            @Override
            public void onSubscribe(@NonNull Disposable d) {
                AntLog.i("onSubScribe==");
            }

            @Override
            public void onNext(@NonNull String s) {
                AntLog.i("value==" + s);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                AntLog.i("onError==" + e.getMessage());
            }

            @Override
            public void onComplete() {
                AntLog.i("onComplete()");
            }
        };

        observable.subscribe(observer);

    }

    public void test4() {

        new Observable<String>() {

            @Override
            protected void subscribeActual(Observer<? super String> observer) {
                observer.onNext("Hello");
                observer.onNext("Word");
                observer.onNext("Antasdlfkasdfjsa");
                observer.onNext("skaweklsddkfals");
                observer.onComplete();
            }
        }.subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                AntLog.i("onSubScribe==");
            }

            @Override
            public void onNext(@NonNull String s) {
                AntLog.i("value==" + s);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                AntLog.i("onError==" + e.getMessage());
            }

            @Override
            public void onComplete() {
                AntLog.i("onComplete()");
            }
        });
    }

    /**
     * main and subThread to here
     */
    public void test5() {
        Observable<String> observable = new Observable<String>() {
            @Override
            protected void subscribeActual(Observer<? super String> observer) {
                observer.onNext("lalalal");
                AntLog.i("thread name=" + Thread.currentThread().getName());
                observer.onNext("wolilalalalal");
                observer.onComplete();
            }
        };
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(@NonNull String s) throws Exception {
                AntLog.i("value==" + s);
                text.setText("ss====" + s);
                AntLog.i("observer thread name=" + Thread.currentThread().getName());
            }
        };

        observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer);
    }


    public void test6() {

        new Retrofit.Builder()
                .baseUrl("http://www.runoob.com/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(Api.class)
                .login("user")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        AntLog.i("data==" + s);
                        text.setText(s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        AntLog.i("login error");
                    }

                    @Override
                    public void onComplete() {
                        AntLog.i("login sucessful");
                    }
                });

    }


    public interface Api {
        @GET("/")
        Observable<String> login(@Query("id") String id);

        @GET("/")
        Observable<String> regist(@Query("id") String id);
    }

}
